package com.fs.controller;

import com.fs.pojo.Inventory;
import com.fs.pojo.Outsto;
import com.fs.pojo.SalesDetails;
import com.fs.service.InventoryToService;
import com.fs.service.OutstoService;
import com.fs.service.SalesDetailsService;
import com.fs.util.ResponseDataUtil;
import com.fs.vo.Condition;
import com.fs.vo.ResponseData;
import com.fs.vo.ResultEnums;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author python_boy
 * @fileName InventoryController
 * @date 2024/10/17 19:19
 * @description
 **/
@RestController
@RequestMapping("/sales/inventory")
public class InventoryController {
    @Autowired
    private InventoryToService inventoryToService;

    @Autowired
    private SalesDetailsService salesDetailsService;

    @Autowired
    private OutstoService outstoService;
    /**
     * 根据条件查询库存单列表（分页）
     * @param condition 查询条件
     * @return 包含库存单分页数据的响应
     */
    @PostMapping("/list")
    public ResponseData<PageInfo> selectPage(@RequestBody Condition<Inventory> condition){
        return new ResponseData<PageInfo>(ResultEnums.OK,inventoryToService.selectAll(condition));
    }

    /**
     * 根据库存编号删除库存单
     * @param inventoryId 出库单编号
     * @return 包含出库单数据的响应
     */
    @DeleteMapping("/{inventoryId}")
    public ResponseData<String> deleteOutbound(@PathVariable String inventoryId) {
        try {
            if (inventoryId.contains(",")) {
                // 批量删除
                String[] ids = inventoryId.split(",");
                for (String id : ids) {
                    inventoryToService.deleteInventory(Integer.parseInt(id));
                }
                return new ResponseData<>(ResultEnums.OK, "批量删除成功");
            } else {
                // 单个删除
                inventoryToService.deleteInventory(Integer.parseInt(inventoryId));
                return new ResponseData<>(ResultEnums.OK, "删除成功");
            }
        } catch (Exception e) {
            return new ResponseData<>(ResultEnums.DELETED, null);
        }
    }

    /**
     * 根据库存编号修改出库单。
     *
     * @param inventory 库存单对象
     * @return 包含库存单数据的响应
     */
    @PutMapping("/update")
    public ResponseData updateInventory(@RequestBody Inventory inventory){
        Integer result = inventoryToService.updateInventory(inventory);
        return result!=0? ResponseDataUtil.buildOk("200","修改成功"):ResponseDataUtil.buildFail(null);
    }

    /**
     * 添加库存审批单
     * @param outsto 库存单对象
     * @return 包含库存单数据的响应
     */
    @PostMapping("/addApproval")
    public ResponseData addInventory(@RequestBody Outsto outsto){
        Integer result = outstoService.addOutsto(outsto);
        return result!=0? ResponseDataUtil.buildOk("200","审批发送成功"):ResponseDataUtil.buildFail(null);
    }
}
